<?php

namespace app\api\controller;

use app\common\controller\Logined;

use app\common\service\SmsService as Sms;
use app\common\service\WalletService;
use app\common\service\MailService as Mail;
use app\common\service\TranslateService;
use think\Db;

class User extends Logined {

	/*
	获取用户信息
	*/	
	public function getUserInfo(){
		$uid = $this->uid;
		if (empty($uid)) {
			$this->retData(1, '缺少参数');
		}
		$data    = $this->user_info;
		if(empty($data)){
			$this->retData(0, 'fail');
		}
		unset($data['password']);
		unset($data['paypass']);
		//等级

		$level = $data['level'];
		if ($level == 0) {
			$data['level_name'] = Db::table('tp_invest_level_config')->where('level',$level)->value('name');
			//$data['level_name'] = '普通会员';
		} else {
			$data['level_name'] = Db::table('tp_invest_level_config')->where('level',$level)->value('name');
		}  
		          //翻译
		          
        $data['level_name'] = TranslateService::baiduApi($data['level_name'],$this->lang);
		$robot_level = $data['robot_level'];
		// var_dump($robot_level);die();
		$data['robot_name'] = Db::table('tp_invest_config')->where('id',$robot_level)->value('name');
		$data['robot_name'] = TranslateService::baiduApi($data['robot_name'],$this->lang);
		switch ($data['identity_status']) {
            case 0:
                $data['identity'] = '点击认证';
                break;
            case 1:
                $data['identity'] = '审核中';
                break;
            case 2:
                $data['identity'] = '已认证';
                break;
            case 3:
                $data['identity'] = '认证失败';
                break;
            default:
                $data['identity'] = '点击认证';
                break;
        }
        $data['words_status'] = model('UserRecoveryModel')->getWords($uid)['status'];
		$this->retData(0,'success',$data);
	}

	/*
	获取支付信息
	 */
	public function getPayInfo() {
		$uid = $this->uid;
		if (empty($uid)) {
			$this->retData(1, '缺少参数');
		}
		$data = model('PayInfoModel')->getPayInfo($uid);
		if ($data) {
			$this->retData(0,'success',$data);
		} else {
			$this->retData(0, 'fail');
		}
	}


	/*
	绑定手机
	 */
	public function bindPhone(){
		$uid = $this->uid;
		$phone = P('phone');
		$quhao = P('quhao')?:'86';
		$code  = P('code');
		if (empty($uid) || empty($phone) || empty($code)) {
			$this->retData(1, '缺少参数');
		}
		// $phone_verify = model('UserModel')->infoByPhone($phone);
		// if ($phone_verify) {
		// 	$this->retData(1, '手机号码已被注册，请更换手机号码！');
		// }
		$user_num = model('UserModel')->countPhone($phone);
		if ($user_num >= C('register_phone_max')) {
		 	$this->retData(1, '单个电话号码最多绑定' . C('register_phone_max') . '个账号');
		}
		//短信开关
		if (C('sms_verify_button') == 1) {
			$veri_code = model('PhoneCodeModel')->getPhoneCode($phone, 1);
			if ($code != $veri_code) {
				$this->retData(1, '验证码错误或者已过期');
			}
		}
		$data = [
			'phone' =>$phone,
			'quhao' =>$quhao
		];
		$res = model('UserModel')->updateUser($uid,$data);
		if ($res) {
			$this->retData(0, '设置成功');
		} else {
			$this->retData(1, '设置失败');
		}
	}

	/*
	绑定邮箱
	 */
	public function bindMail(){
		$uid = $this->uid;
		$mail = P('mail');
		$code  = P('code');
		if (empty($uid) || empty($mail) || empty($code)) {
			$this->retData(1, '缺少参数');
		}
		$mail_verify = model('UserModel')->infoByMail($mail);
		if ($mail_verify) {
			$this->retData(1, '邮箱已被注册，请更换邮箱！');
		}
		if (C('mail_verify_button') == 1) {
			$veri_code = model('MailCodeModel')->getMailCode($mail, 1);
			if ($code != $veri_code) {
				$this->retData(1, '验证码错误或者已过期');
			}
		}
		$data = [
			'mail' =>$mail,
		];
		$res = model('UserModel')->updateUser($uid,$data);
		if ($res) {
			$this->retData(0, '设置成功');
		} else {
			$this->retData(1, '设置失败');
		}
	}

	/*
	  设置微信
	 */
	public function setWeixin() {
		$uid         = $this->uid;
		$weixin      = P('weixin');
		$weixin_code = P('weixin_code');
		if (empty($uid) || !isset($weixin) || !isset($weixin_code)) {
			$this->retData(1, '缺少参数');
		}
		$data = [
			'weixin' => $weixin,
			'weixin_code' => $weixin_code,
		];
		$res = model('PayInfoModel')->setPayInfo($uid,$data);
		if ($res) {
			$this->retData(0, '设置成功');
		} else {
			$this->retData(1, '设置失败');
		}
	}

	/*
	设置支付宝
	*/
	public function setAlipay() {
		$uid         = $this->uid;
		$alipay      = P('alipay');
		$alipay_code = P('alipay_code');
		if (empty($uid) || !isset($alipay) || !isset($alipay_code)) {
			$this->retData(1, '缺少参数');
		}
		$data = [
			'alipay'      => $alipay,
			'alipay_code' => $alipay_code,
		];
		$res = model('PayInfoModel')->setPayInfo($uid,$data);
		if ($res) {
			$this->retData(0, '设置成功');
		} else {
			$this->retData(1, '设置失败');
		}
	}

	/*
	设置云闪付
	*/
	public function setCloud() {
		$uid        = $this->uid;
		$cloud      = P('cloud');
		$cloud_code = P('cloud_code');
		if (empty($uid) || !isset($cloud) || !isset($cloud_code)) {
			$this->retData(1, '缺少参数');
		}
		$data = [
			'cloud'      => $cloud,
			'cloud_code' => $cloud_code,
		];
		$res = model('PayInfoModel')->setPayInfo($uid,$data);
		if ($res) {
			$this->retData(0, '设置成功');
		} else {
			$this->retData(1, '设置失败');
		}
	}


	/*
	  设置银行
	 */
	public function setBank() {
		$uid        = $this->uid;
		$card       = P('card');
		$card_bank  = P('card_bank');
		$card_name  = P('card_name');
		$card_phone = P('card_phone');
		if (empty($uid) || !isset($card) || !isset($card_bank) || !isset($card_name) || !isset($card_phone)) {
			$this->retData(0, '缺少参数');
		}
		$data = [
			'card'       => $card,
			'card_bank'  => $card_bank,
			'card_name'  => $card_name,
			'card_phone' => $card_phone
		];
		$res = model('PayInfoModel')->setPayInfo($uid,$data);
		if ($res) {
			$this->retData(0, '设置成功');
		} else {
			$this->retData(1, '设置失败');
		}
	}

	/*
	  获取认证信息
	 */
	public function getIdCard() {
		$uid = $this->uid;
		if (empty($uid)) {
			$this->retData(1, '缺少参数');
		}
		$data = model('UserModel')->getUserInfo($uid);
		if ($data['identity_status'] != 0) {
			$this->retData(0,'success',$data);
		} else {
			$this->retData(0,'fail');
		}
	}

	/*
	设置认证信息
	*/
	public function setIdCard() {
		$uid          = $this->uid;
		$real_name    = P('real_name');
		$idcard_no    = P('idcard_no');
		$idcard_front = P('idcard_front');
		$idcard_back  = P('idcard_back');
		if (empty($uid) || empty($real_name) || empty($idcard_no) || empty($idcard_front) || empty($idcard_back)) {
			$this->retData(1, '缺少参数');
		}
		if (strlen($idcard_no) != 18) {
			$this->retData(1, '身份证号码错误');
		}
		$has = Db::table('tp_user')->where('idcard_no',$idcard_no)->find();
		if($has){
			if($has['id'] != $uid){
				$this->retData(1, '该身份信息已绑定用户');
			} else {
				if($has['identity_status'] == 2){
					$this->retData(1, '已认证，不可编辑');
				}
			}
		}
		$data = [
			'real_name'      =>$real_name,
			'idcard_no'      =>$idcard_no,
			'idcard_front'   =>$idcard_front,
			'idcard_back'    =>$idcard_back,
			'identity_status'=>1
		];
		$res = model('UserModel')->updateUser($uid,$data);
		if ($res) {
			$this->retData(0, '设置成功');
		} else {
			$this->retData(1, '设置失败');
		}
	}

	/*
	修改登录密码
	*/
	public function forgetPass() {
		$uid = $this->uid;
		$password = P('password');
		$code = P('code');
		$type = P('type')?:3;
		if (empty($uid) || empty($password) || empty($code)) {
			$this->retData(1, '缺少参数');
		}

		$user_info = $this->user_info;
		
		$match_password = '/^[0-9_a-zA-Z]{8,20}$/';
		if (!preg_match($match_password, $password)) {
			$this->retData(1, '登录密码8-20位,且只能为字母数字下划线组合');
		}
		if($type==1){
			//短信开关
			if (C('sms_verify_button') == 1) {
				if(empty($user_info['phone'])){
					$this->retData(1, '未绑定手机号码');
				}
				$veri_code = model('PhoneCodeModel')->getPhoneCode($user_info['phone'],1);
				if ($code != $veri_code) {
					$this->retData(1, '验证码错误或者已过期');
				}
			}
		} elseif($type == 2) {
			if(empty($user_info['mail'])){
				$this->retData(1, '未绑定邮箱');
			}
			$veri_code = model('MailCodeModel')->getMailCode($user_info['mail'], 1);
			if ($code != $veri_code) {
				$this->retData(1, '验证码错误或者已过期');
			}
		}
		$password = md5($password);
		if ($password == $user_info['password']) {
			$this->retData(1, '新密码和旧密码一致');
		}
		$data = [
			'password'=>$password
		];
		$res = model('UserModel')->updateUser($uid,$data);
		if ($res) {
			$this->retData(0, '修改成功');
		} else {
			$this->retData(1, '修改失败');
		}
	}

	/*
	交易密码更改
	*/
	public function forgetPayPass() {
		$uid = $this->uid;
		$paypass = P('paypass');
		$code = P('code');
		$type = P('type')?:3;
		if (empty($uid) || empty($paypass) || empty($code)) {
			$this->retData(1, '缺少参数');
		}
		$user_info = $this->user_info;

		$match_paypass = '/^[0-9]{6}$/';
		if (!preg_match($match_paypass, $paypass)) {
			// $this->retData(1, '交易密码为6位纯数字');
		}
		if($type==1){
			//短信开关
			if (C('sms_verify_button') == 1) {
				if(empty($user_info['phone'])){
					$this->retData(1, '未绑定手机号码');
				}
				$veri_code = model('PhoneCodeModel')->getPhoneCode($user_info['phone'],1);
				if ($code != $veri_code) {
					$this->retData(1, '验证码错误或者已过期');
				}
			}
		} elseif($type == 2) {
			if(empty($user_info['mail'])){
				$this->retData(1, '未绑定邮箱');
			}
			$veri_code = model('MailCodeModel')->getMailCode($user_info['mail'], 1);
			if ($code != $veri_code) {
				$this->retData(1, '验证码错误或者已过期');
			}
		}
		
		if ($paypass == $user_info['paypass']) {
			$this->retData(1, '新密码和旧密码一致');
		}
		$data = [
			'paypass'=>$paypass
		];
		$res = model('UserModel')->updateUser($uid,$data);
		if ($res) {
			$this->retData(0, '修改成功');
		} else {
			$this->retData(1, '修改失败');
		}
	}

	/*
	设置头像
	 */
	public function setAvatar(){
		$avatar = P('avatar');
		if (empty($avatar)) {
			$this->retData(1, '缺少参数');
		}
		$uid = $this->uid;
		$data = [
			'avatar'=>$avatar
		];
		$res = model('UserModel')->updateUser($uid,$data);
		if ($res) {
			$this->retData(0, '设置成功');
		} else {
			$this->retData(1, '设置失败');
		}
	}

	/*
	助记词
	*/
	public function getWords(){
		$uid = $this->uid;
		$paypass = P('paypass');
		if (empty($uid) || empty($paypass)) {
			$this->retData(1, '缺少参数');
		}
		$user_paypass = model('UserModel')->getPaypass($uid);
		if($user_paypass != $paypass){
			$this->retData(1,'交易密码错误');
		}
		$words = model('UserRecoveryModel')->getWords($uid)['words'];
		$this->retData(0,'success',$words);
	}

	/*
	备份助记词
	 */
	public function copyWords(){
		$words = P('words');
		$uid = $this->uid;
		if (empty($uid)  || empty($words)) {
			$this->retData(1, '缺少参数');
		}
		$check_words = model('UserRecoveryModel')->getWords($uid)['words'];
		if($words == $check_words){
			$res =1;
			// $res = model('UserRecoveryModel')->copyWords($uid);
			if($res){
			$this->retData(0,'备份成功');
			} else {
				$this->retData(1,'备份失败');
			}
		}else {
			$this->retData(1,'助记词错误');
		}
	}


	public function getInviteUrl(){
		$uid = $this->uid;
		if (empty($uid)) {
			$this->retData(1, '缺少参数');
		}
		$invite_code = $this->user_info['invite_code'];
		$system_url = C('reg_url');
		$data = $system_url.'?lang='.$this->lang.'&invite_code='.$invite_code;
		$this->retData(0,'success',$data);
	}


	/*
	  短信接口
	 */
	public function sms() {
		$uid = $this->uid;
		$type = P('type') ?: 2;
		if (empty($uid)) {
			$this->retData(1, '缺少参数');
		}
		$type = 1;
		$user_info = model('UserModel')->getUserInfo($uid);
		$res = Sms::sms($user_info['quhao'],$user_info['phone'], $type);
		if ($res['code'] == 0) {
			$this->retData(0, '发送成功');
		} else {
			$this->retData(1, $res['msg']);
		}
	}

	/*
	邮箱接口
	*/
	public function mail() {
		$uid = $this->uid;
		$type = P('type') ?: 2;
		if (empty($uid)) {
			$this->retData(1, '缺少参数');
		}
		$type = 1;
		$user_info = model('UserModel')->getUserInfo($uid);
		$res = Mail::mail($user_info['mail'], $type);
		if ($res['code'] == 0) {
			$this->retData(0, '发送成功');
		} else {
			$this->retData(1, $res['msg']);
		}
	}
	/*
	获取设备ID
	*/
	public function getRid(){
		$uid = $this->uid;
		$rid = P('rid');
		if (empty($uid) || empty($rid)) {
			$this->retData(1, '缺少参数');
		}
		$data = [
			'rid'=>$rid
		];
		$re = model('UserModel')->updateUser($uid,$data);
		//$res = APush::user_sync($this->uid , $rid);
		if ($re) {
			$this->retData(0, 'success');
		}else {
			$this->retData(0, 'fail');
		}
	}
}
